//
//  XUEmotion.m
//  新浪微博
//
//  Created by xuwenxin on 15-4-18.
//  Copyright (c) 2015年 xuwenxin. All rights reserved.
//

#import "XUEmotion.h"
#import "XUEmotionModel.h"
#import "NSString+Emoji.h"
#import "XUEmoticonSectionModel.h"

@implementation XUEmotion


+ (NSMutableArray *)Emotion
{
    NSString *path = [[NSBundle mainBundle] pathForResource:@"Emoticons/emoticons.plist" ofType:nil];

    NSString *emotioconspath = [[NSBundle mainBundle] pathForResource:@"Emoticons" ofType:nil];
    //    NSLog(@"emotioconspath = %@",emotioconspath);
    //    NSLog(@"%@",path);
    // 表情文件夹 ,  一共有4个
    // 最大的数组
    NSMutableArray *Allarray = [NSMutableArray array];
    // 总的plist文件转为数组
    NSArray *array = [NSArray arrayWithContentsOfFile:path];
    // 4个文件夹
    for (NSDictionary *dict in array) {
        // 每个文件夹的姓名
        NSString *pathPath = dict[@"emoticon_group_path"];
        // 每组表情的名称
        NSString *titleName = dict[@"emoticon_group_name"];
        //        NSLog(@"path%@",pathPath);
        NSMutableString *mutablePath = [emotioconspath mutableCopy];
        
        //  可拼接的图片路径   [mutablePath appendFormat:@"%@",pathPath];
        NSMutableString *imgPath = [emotioconspath mutableCopy];
        [imgPath appendFormat:@"/%@/",pathPath];
        
        [mutablePath appendFormat:@"/%@/info.plist",pathPath];
        //        NSLog(@"mutablePath == %@",mutablePath);
        // 全部是字典 放入到可变数组中
        [Allarray addObjectsFromArray:[[self alloc] arrayEmotionPath:mutablePath imgPath:imgPath sectionTitle:titleName]];
//        [Allarray addObject:[[self alloc] arrayEmotionPath:mutablePath]];
    }
//    NSLog(@"Allarray%@",Allarray);
//    NSLog(@"%tu",Allarray.count);
    return Allarray;
}
/**
 *  遍历进行拼接
 *
 *  @param path         plist的文件全路径
 *  @param imgPath      表情图片的可拼接全路径
 *  @param sectionTitle 每组表情的名称
 *
 *  @return 返回每个文件夹以20个表情一组整理好的数组
 */
- (NSArray *)arrayEmotionPath:(NSString *)path imgPath:(NSString *)imgPath sectionTitle:(NSString *)sectionTitle
{
    //    NSLog(@"path ==== %@",path);
    // 去除每个文件夹里面的字典
    NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:path];
    // 字典里面有个key是emoticon_group_emoticons 他得value是数组,,取出数组
    NSArray *array2 = dict[@"emoticon_group_emoticons"];
    XUEmotionModel *nilModel = [[XUEmotionModel alloc] init];
    nilModel.code = @"";
    nilModel.type = @"1";
    NSMutableArray *sectionArray = [NSMutableArray array];
    NSMutableArray *itemArray = [NSMutableArray array];
    // 遍历数组 每21个数组包装成一个数组 然后再包装成一个大得数组
    NSInteger count = (array2.count / 21) + 1;
    for (int i = 1; i < (21 * count) + 1; i++) {
        // 数组元素
        if ( i < array2.count + 1) {
            // 初始化图片路径用来拼接
            NSMutableString *img = [imgPath mutableCopy];
            // 转为模型
            XUEmotionModel *model = [XUEmotionModel objectWithKeyValues:array2[i-1]];
            if (model.png) {
                [img appendString:model.png];
                // 拼接为全路径
                model.imgPath = img;
            }
            
            if (model.code) {
                // 转换表情字符
                model.code = [model.code emoji];
            }
            // 把每个模型放到可变数组中
            [itemArray addObject:model];
        } else { // 如果不够21个用空得数组补齐
            [itemArray addObject:nilModel];
        }
        

        // 判断每21个表情就把可变数组放到大数组中,这里写20是因为最后一个是删除键
        if ( (i % 20 == 0)) {
            // 到这里说明够21个,因为最后一个是删除键,所以用空模型代替
            XUEmotionModel *nilModel = [[XUEmotionModel alloc] init];
            nilModel.png = @"compose_emotion_delete";
            nilModel.type = @"3";
            [itemArray addObject:nilModel];
            
            /*
             为了给每一组增加一个标题,
             这里又包装了一个模型
             */
            XUEmoticonSectionModel *sectionModel = [[XUEmoticonSectionModel alloc] init];
            sectionModel.sectionArray = [itemArray copy];
            sectionModel.sectionTitle = sectionTitle;
            
            // 将这21个模型 包装成数组 放到大数组中
            [sectionArray addObject:sectionModel];
            
            // 清空可变数组
            [itemArray removeAllObjects];
        }
    }
    //    NSLog(@"dict == %@", dict);
    //    NSLog(@"array2 == %@",  array2);
    // 返回组的数组,,一共返回4次
    return sectionArray;
}

@end
